package com.solcoo.common.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Description
 * 
 * @author panjunlin
 * 
 * @version 创建时间：2012-9-26 下午3:35:28
 * 
 */
public final class ArrayUtil {

	public static final String[] EMPTY_STRING_ARRAY = new String[0];

	public static final int INDEX_NOT_FOUND = -1;

	public static boolean isEmpty(Object[] arrays) {
		return arrays == null || arrays.length == 0;
	}

	public static int getLength(Object[] arrays) {
		return isEmpty(arrays) ? 0 : arrays.length;
	}

	public static boolean isNotEmpty(Object[] arrays) {
		return !isEmpty(arrays);
	}

	public static <T> T get(T[] arrays, int index) {
		return isEmpty(arrays) || index >= arrays.length ? null : arrays[index];
	}

	public static <T> T getFrist(T[] arrays) {
		return isEmpty(arrays) ? null : arrays[0];
	}

	public static <T> T getLast(T[] arrays) {
		return isEmpty(arrays) ? null : arrays[arrays.length - 1];
	}

	public static <T> T[] subarray(T[] array, int startIndexInclusive,
			int endIndexExclusive) {
		if (array == null) {
			return null;
		}
		if (startIndexInclusive < 0) {
			startIndexInclusive = 0;
		}
		if (endIndexExclusive > array.length) {
			endIndexExclusive = array.length;
		}
		int newSize = endIndexExclusive - startIndexInclusive;
		Class<?> type = array.getClass().getComponentType();
		if (newSize <= 0) {
			@SuppressWarnings("unchecked")
			// OK, because array is of type T
			final T[] emptyArray = (T[]) Array.newInstance(type, 0);
			return emptyArray;
		}
		@SuppressWarnings("unchecked")
		// OK, because array is of type T
		T[] subarray = (T[]) Array.newInstance(type, newSize);
		System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
		return subarray;
	}

	public static int indexOf(long[] array, long valueToFind) {
		return indexOf(array, valueToFind, 0);
	}

	public static int indexOf(long[] array, long valueToFind, int startIndex) {
		if (array == null) {
			return INDEX_NOT_FOUND;
		}
		if (startIndex < 0) {
			startIndex = 0;
		}
		for (int i = startIndex; i < array.length; i++) {
			if (valueToFind == array[i]) {
				return i;
			}
		}
		return INDEX_NOT_FOUND;
	}

	public static <T> ArrayList<T> asList(T... ts) {
		if (ts == null) {
			return new ArrayList<T>();
		}
		return new ArrayList<T>(Arrays.asList(ts));
	}

	public static int indexOf(int[] array, int valueToFind, int startIndex) {
		if (array == null) {
			return INDEX_NOT_FOUND;
		}
		if (startIndex < 0) {
			startIndex = 0;
		}
		for (int i = startIndex; i < array.length; i++) {
			if (valueToFind == array[i]) {
				return i;
			}
		}
		return INDEX_NOT_FOUND;
	}

	public static int indexOf(int[] array, int valueToFind) {
		return indexOf(array, valueToFind, 0);
	}

	public static boolean contains(int[] array, int valueToFind) {
		return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
	}

	public static int[] addAll(int[] array1, int... array2) {
		if (array1 == null) {
			return clone(array2);
		} else if (array2 == null) {
			return clone(array1);
		}
		int[] joinedArray = new int[array1.length + array2.length];
		System.arraycopy(array1, 0, joinedArray, 0, array1.length);
		System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
		return joinedArray;
	}

	public static int[] clone(int[] array) {
		if (array == null) {
			return null;
		}
		return (int[]) array.clone();
	}
}
